//
//  PhotoBrowserModalAnimation.m
//  XinLang_OC
//
//  Created by Boss Lin on 16/6/14.
//  Copyright © 2016年 cheuhui. All rights reserved.
//

#import "PhotoBrowserModalAnimation.h"
#import "PhotoBrowserVC.h"

@implementation PhotoBrowserModalAnimation

#pragma mark - UIViewControllerAnimatedTransitioning

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.25;
}

// 实现这个方法后,需要手动添加modal出来控制的view到容器视图上面,还要通知系统转场动画完成
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    // 获取modal出来的控制器的view
    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    
    // 添加到容器视图
    [[transitionContext containerView] addSubview:toView];
    
    // 获取modal出来的控制器
    PhotoBrowserVC *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    // 获取过滤视图
    UIView *tempImageView = [toViewController modalTempImageView];
    
    // 添加到容器视图
    [[transitionContext containerView] addSubview:tempImageView];
    
    // 隐藏collectionView
    toViewController.collectionView.hidden = YES;
    
    // 淡入
    toView.alpha = 0;
    
    [UIView animateWithDuration:0.5 animations:^{
        // 动画到大图的位置
        tempImageView.frame = [toViewController modalTargetFrame];
        toView.alpha = 1;
    } completion:^(BOOL finished) {
        // 显示collectionView
        toViewController.collectionView.hidden = NO;
        // 移除过滤视图
        [tempImageView removeFromSuperview];
        // 一定要记住调用，告诉系统转场完成
        [transitionContext completeTransition:YES];
    }];
}



@end
